<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\AuditPassBtn;
use App\Admin\Extensions\AuditRejectBtn;
use Encore\Admin\Controllers\AdminController;
use App\Models\SchoolInfo;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class ApplyController extends AdminController
{
    protected $title = '申请';
    private $statusMap = [
        SchoolInfo::STATUS_PENDING_REVIEW => '未审核',
        SchoolInfo::STATUS_PASS => '通过',
        SchoolInfo::STATUS_REJECT => '已驳回',
    ];

    /**
     * 开通学校申请列表
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new SchoolInfo);
        $statusMap = $this->statusMap;
        $grid->column('id', __('ID'));
        $grid->column('owner.name', __('申请者'));
        $grid->column('owner.email', __('申请邮箱'));
        $grid->column('school_name', __("校名"));
        $grid->column('address', __("地址"));
        $grid->column('status', "审核状态")->using($statusMap);
        $grid->column('created_at', __("申请时间"));
        $grid->paginate(20);
        $grid->perPages([20, 30, 50]);
        $grid->disableCreateButton();

        $grid->filter(function ($filter) use ($statusMap){
            $filter->disableIdFilter();
            $filter->like('owner.name', '申请者');
            $filter->equal('owner.email', '申请邮箱');
            $filter->equal("status", '审核状态')->select($statusMap);

        });
        $grid->actions(function ($actions) {
            if ($actions->getAttribute('status') == SchoolInfo::STATUS_PENDING_REVIEW) {
                $actions->add(new AuditPassBtn());
                $actions->add(new AuditRejectBtn());
            }
            // 去掉删除
            $actions->disableDelete();
            // 去掉编辑
            $actions->disableEdit();
        });
        return $grid;
    }

    /**
     * 开通学校申请详情
     *
     * @param $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(SchoolInfo::findOrFail($id));
        $statusMap = $this->statusMap;

        $show->field('id', 'ID');
        $show->field('owner.name', '申请者');
        $show->field('owner.email', '申请邮箱');
        $show->field('school_name', '校名');
        $show->field('address', '地址');
        $show->field('status', "审核状态")->using($statusMap);
        $show->field('created_at');
        $show->panel()
            ->tools(function ($tools) {
                $tools->disableEdit();
                $tools->disableDelete();
            });
        return $show;
    }


}